/******************************************************************************
 * Copyright (C) 671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODECPP_NET_TCPSOCKET_H_
#define NODECPP_NET_TCPSOCKET_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstddef>
#include <cstdint>
#include <string>
#include <event2/event.h>
#include <event2/util.h>
#include <event2/bufferevent.h>
#include <NodeCpp/Macros.h>
#include <NodeCpp/AutoPtr.h>

namespace NodeCpp {
namespace Net {
    class TcpSocketDelegate;
    class TcpRunnable;
    class TcpServer;

    class TcpSocket
    {
    public:
        enum EventFlags {
            EF_READ = 0x1, EF_WRITE = 0x2
        };
        enum ShutdownHow {
            SH_READ = 0x1, SH_WRITE = 0x2
        };

        TcpSocket(void);
        ~TcpSocket(void);

        bool Open(struct event_base *_Base, evutil_socket_t _Sock, struct sockaddr *_SockAddr = NULL, int _AddrLen = 0);
        void Close(void);

        int id(void) const { return ID_; }
        const std::string& host(void) const { return Host_; }
        int port(void) const { return Port_; }

        const std::string& GetErrorString(void) const { return ErrorString_; }
        int GetErrorCode(void) const { return ErrorCode_; }

        void Send(const std::uint8_t *_Data, std::size_t _Size);

        std::size_t GetReceivedSize(void);
        int Read(std::uint8_t *_Data, std::size_t _Size);
		std::size_t ReadPeek(std::uint8_t *_Data, std::size_t _Size);
        std::size_t ReadSkip(std::size_t _Size);

        void EnableReceive(bool _Enable);
        void EnableSend(bool _Enable);
        void Shutdown(int _How);

    private:
        friend class TcpServer;

        void _SetId(int _Id) { ID_ = _Id; }
        void _SetOwner(TcpServer *_Owner) { Owner_ = _Owner; }
        void _SetThread(TcpRunnable *_Runnable) { Runnable_ = _Runnable; }
        void _SetDelegate(TcpSocketDelegate *_Delegate) { Delegate_.reset(_Delegate); }
        void _Unintialize(void);

        void _OnError(void);

        static void _ReadCallback(struct bufferevent *, void *);
        static void _WriteCallback(struct bufferevent *, void *);
        static void _EventCallback(struct bufferevent *, short, void *);

        int ID_;
        evutil_socket_t Sock_;
        struct bufferevent *Bev_;
        AutoPtr<TcpSocketDelegate> Delegate_;
        std::string Host_;
        int Port_;
        std::string ErrorString_;
        int ErrorCode_;
        volatile bool FatalError_;
        TcpServer *Owner_;
        TcpRunnable *Runnable_;
        DISALLOW_COPY_AND_ASSIGN(TcpSocket);
    };

}}

#endif // NODECPP_NET_TCPSOCKET_H_
